using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Socket_Client
{
    class App
    {
        IPAddress RemoteIP = IPAddress.Parse("127.0.0.1");
        int Port = 2214;
        string SendContent = "0123456789";
        int BufferSize = 1024;
        int StartReceiveServiceErrorRetryCount = 10;
        int SendingInterval = 1000;
        Timer SendingTimer;
        int SentCount = 0;
        int ReceivedCount = 0;


        public void Run(string[] args)
        {
            InitParam(args);

            Console.WriteLine("按回车键结束.");

            using (AsyncSocket aAsyncSocket = new AsyncSocket(Port, BufferSize, StartReceiveServiceErrorRetryCount))
            {
                // BindSocketLogger(aAsyncSocket);

                aAsyncSocket.OnReceived += (AsyncSocket asyncSocket, SocketState state) => { ReceivedCount++; };

                aAsyncSocket.StartReceiving();
                SendingTimer = new Timer((object state) =>
                   {
                       Console.WriteLine("发送 {0} 次, 收到回复 {1} 次", SentCount, ReceivedCount);
                       aAsyncSocket.SendTo(RemoteIP, SendContent);
                       SentCount++;
                   },
                null, SendingInterval, SendingInterval);
                Console.ReadLine();
            }

        }

        void InitParam(string[] args)
        {
            using (Params aParams = new Params())
            {
                aParams.Parse(args);

                RemoteIP = aParams.GetParamValue("-RemoteIP", RemoteIP);
                Console.WriteLine("RemoteIP: {0}", RemoteIP);
                Port = aParams.GetParamValue("-PORT", Port);
                Console.WriteLine("Port: {0}", Port);
                SendContent = aParams.GetParamValue("-SENDCONTENT", SendContent);
                Console.WriteLine("SendContent: {0}", SendContent);
                BufferSize = aParams.GetParamValue("-BUFFERSIZE", BufferSize);
                Console.WriteLine("BufferSize: {0}", BufferSize);
            }
        }

        void BindSocketLogger(AsyncSocket aAsyncSocket)
        {
            aAsyncSocket.OnWaitingReceive += (AsyncSocket sender, SocketState state) =>
            {
                Console.WriteLine("{0:yyyy-MM-dd HH:mm:ss} Attempting to Receive data from 0.0.0.0:{1}", DateTime.Now, Port);
            };

            aAsyncSocket.OnSent += (AsyncSocket sender, SocketState state) =>
            {
                Console.WriteLine("{0:yyyy-MM-dd HH:mm:ss} Sent {1} bytes to {2}", DateTime.Now, state.MessageLength, state.RemoteEndPoint);
            };

            aAsyncSocket.OnReceivingError += (AsyncSocket sender, SocketState state, string message) =>
            {
                Console.WriteLine("{0:yyyy-MM-dd HH:mm:ss} Received error for {1}, Message: {2}", DateTime.Now, state.RemoteEndPoint, message);
            };

            aAsyncSocket.OnSendError += (AsyncSocket sender, SocketState state, string message) =>
            {
                Console.WriteLine("{0:yyyy-MM-dd HH:mm:ss} Send error for {1}, Message: {2}", DateTime.Now, state.RemoteEndPoint, message);
            };
        }
    }
}
